PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on Loops

Program to find HCF / GCD of two number

Input - Output

Input
Enter two number to find HCF : 12 15

Output
HCF of 12 and 15 : 3

Algorithm

Step 1: START

Step 2: declare vaiable num1 , num2, HCF(to store our result).

Step 3: Input two number from user and store into num1 and num2.

Step 4: Now, we have to find a smallest number from that num1 and num2 divide exactly.

Step 5: END.

CODE


/* Find HCF/GCD of two number */

#include<stdio.h>
int main()
{
      int num1,num2,HCF;
      printf(" Enter the two number to find HCF : ");
      scanf("%d%d",&num1,&num2);
      for(int i=1;i<=num1 && i<=num2;i++)
        if(num1 % i == 0 && num2 % i == 0)
        HCF = i;
        printf(" HCF of %d and %d : %d",num1,num2,HCF);
    return 0;
}


/* Find HCF/GCD of two number */

#include<iostream>
using namespace std;
int main()
{
      int num1,num2,HCF;
      cout<<" Enter the two number to find HCF : ";
      cin>>num1>>num2;
      for(int i=1;i<=num1 && i<=num2;i++)
        if(num1 % i == 0 && num2 % i == 0)
        HCF = i;
        cout<<" HCF of "<<num1<<" and "<<num2<<" : "<<HCF;
    return 0;
}

OutPut

Enter two number to find HCF : 12 15

HCF of 12 and 15 are = 3